package com.kingyun.gpsinspection.purificationservice.services.activitiservice.bpm;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.kingyun.gpsinspection.purificationservice.common.utils.CommonUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.activiti.BpmConfNodeMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.activiti.BpmConfNode;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.activiti.bpm.BpmConfNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 流程节点配置类ServiceImpl
 */
public class BpmConfNodeServiceImpl  implements BpmConfNodeService {


       @Autowired
       private  BpmConfNodeMapper bpmConfNodeMapper;


    /**
     * 维护流程节点配置信息
     *
     * @param bpmConfNodes 流程节点集合
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> maintenanceBpmConfNode(EmployeeVo employeeVo, List<BpmConfNode> bpmConfNodes) {
        Map<String,Object> result= Maps.newHashMap();
            int createCount = 0;
            int modifyCount = 0;
            if (bpmConfNodes == null || bpmConfNodes.size() == 0) {
                throw new IllegalArgumentException("配置流程节点参数有误");
            }
            for (BpmConfNode bpmConfNode : bpmConfNodes) {
                //新增流程节点配置信息
                if (Strings.isNullOrEmpty(bpmConfNode.getId())) {
                    bpmConfNode.setId(UniqueUtil.uuid());
                    bpmConfNode.setCreatorId(employeeVo.getEmployeeId());
                    bpmConfNode.setCreateTime(CommonUtil.getTimeString());
                    bpmConfNode.setModifierId(employeeVo.getEmployeeId());
                    bpmConfNode.setModifyTime(CommonUtil.getTimeString());
                    createCount += bpmConfNodeMapper.insertSelective(bpmConfNode);
                } else {
                    //修改流程节点配置信息
                    bpmConfNode.setModifierId(employeeVo.getEmployeeId());
                    bpmConfNode.setModifyTime(CommonUtil.getTimeString());
                    modifyCount += bpmConfNodeMapper.updateSelective(bpmConfNode);
                }
            }
            if ((createCount + modifyCount) == bpmConfNodes.size()) {
                result.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                result.put("msg", "维护流程节点信息成功");
            } else {
                result.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                result.put("msg", "维护流程节点信息失败");
            }
        return result;
    }
}
